'use strict';
const { timeFormater } = require('../utils/tools');
module.exports = app => {
  const { BIGINT, DATE, STRING, INTEGER } = app.Sequelize;
  const LoginHistory = app.model.define('LoginHistory', {
    id: {
      type: BIGINT,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'id',
    },
    createId: {
      type: STRING(20),
      allowNull: true,
      field: 'create_id',
    },
    operationType: {
      type: INTEGER(2),
      allowNull: true,
      defaultValue: '1',
      field: 'operation_type',
    },
    createdTime: {
      type: DATE,
      allowNull: true,
      field: 'created_time',
      get() {
        return timeFormater(this.getDataValue('createdTime'));
      },
    },
    updatedTime: {
      type: DATE,
      allowNull: true,
      field: 'updated_time',
      get() {
        return timeFormater(this.getDataValue('updatedTime'));
      },
    },
  }, {
    tableName: 'login_history',
    timestamps: false,
  });
  LoginHistory.associate = () => {
    // 用户登录退出记录
    LoginHistory.belongsTo(app.model.User, { foreignKey: 'createId', as: 'userHistory' });
  };
  return LoginHistory;
};
